package com.onstar.cn.weight;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.DeviceUtil;

/**
 * @author 7828wc
 * @ClassName: TopLayout
 * @Description: Top Title 栏
 * @date 2016年01月19日 下午1:06:14
 */
public class TopLayout extends LinearLayout {
    private TextView bar_title_id;
    private Button option_home_btn;
    private CustomImageView right_icon;
    private ImageButton option_back_btn;
    private View view;
    private RelativeLayout top_layout_head;
    private boolean isShowClose = false;
    private boolean isShowBack = true;
    private TextView close;
    private int textColor;

    public TopLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public TopLayout(Context context) {
        super(context);
        initView(null);
    }

    public void setLeftButton(int resId) {
        option_back_btn.setImageResource(resId);
    }

    /**
     * @return void?
     * @Title: initView
     * @Description: 初始化View
     * @throws?
     */
    private void initView(AttributeSet attrs) {
        view = inflate(getContext(), R.layout.weight_top_layout, this);
        bar_title_id = (TextView) view.findViewById(R.id.bar_title_id);
        right_icon = (CustomImageView) view.findViewById(R.id.right_icon);
        option_back_btn = (ImageButton) view.findViewById(R.id.option_back_btn);
        option_back_btn.setImageResource(R.drawable.weight_joinme_btnback_selector);
        option_home_btn = (Button) view.findViewById(R.id.option_home_btn);
        option_home_btn.setBackgroundResource(R.drawable.weight_button_feedback_selector);
        top_layout_head = (RelativeLayout) view.findViewById(R.id.top_layout_head);

        close = (TextView) view.findViewById(R.id.close);

//		top_layout_head.setBackgroundResource(R.drawable.weight_bg_header_blue);
        if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
            bar_title_id.setTextSize(11);
        } else {
            bar_title_id.setTextSize(18);
        }
        if (attrs != null) {
            // 对应ATTR
            TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.input);
            String title = typeArray.getString(R.styleable.input_topTitle);
            boolean isShowHome = typeArray.getBoolean(R.styleable.input_isShowHome, true);
            isShowClose = typeArray.getBoolean(R.styleable.input_isShowColse, false);
            isShowBack = typeArray.getBoolean(R.styleable.input_isShowBack, true);
            textColor = typeArray.getInteger(R.styleable.input_textColor, getResources().getColor(R.color.fontColor));

            if (isShowHome) {
                option_home_btn.setVisibility(ImageButton.VISIBLE);
            } else {
                option_home_btn.setVisibility(ImageButton.GONE);
            }
            if (isShowClose) {
                close.setVisibility(View.VISIBLE);
            } else {
                close.setVisibility(View.GONE);
            }
            if (isShowBack) {
                option_back_btn.setVisibility(View.VISIBLE);
            } else {
                option_back_btn.setVisibility(View.GONE);
            }
            //bar_title_id.setTextColor(textColor);
            if (!TextUtils.isEmpty(title)) {
                bar_title_id.setText(title);
            }
            // 释放
            typeArray.recycle();
        }

        option_back_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (getContext() instanceof Activity) {
                    Activity activity = (Activity) getContext();
                    activity.setResult(Activity.RESULT_CANCELED);
                    ((Activity) getContext()).finish();
                }
            }
        });
        option_home_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
    }


    public void showClose(boolean isShowClose) {
        int visible = isShowClose ? View.VISIBLE : View.GONE;
        close.setVisibility(visible);
        if (closeClickListener == null) {
            close.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getContext() instanceof Activity) {
                        Activity activity = (Activity) getContext();
                        activity.finish();
                    } else {
                        close.setOnClickListener(closeClickListener);
                    }
                }
            });
        } else {
            close.setOnClickListener(closeClickListener);
        }
    }

    private View.OnClickListener closeClickListener;

    public void setCloseSetListener(View.OnClickListener clickListener) {
        this.closeClickListener = clickListener;
        close.setOnClickListener(closeClickListener);
    }

    /**
     * @return void?
     * @Title: setLeftButtonClickListener
     * @Description: 设置返回按钮监听
     * @throws?
     */
    public void setLeftButtonClickListener(OnClickListener onClickListener) {
        option_back_btn.setOnClickListener(onClickListener);
    }

    public void setRightButtonClickListener(int strResId, int drawableResId, OnClickListener onClickListener) {
        option_home_btn.setOnClickListener(onClickListener);
        option_home_btn.setBackgroundResource(drawableResId);
        option_home_btn.setText(strResId);
        if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
            option_home_btn.setTextSize(7);
        } else {
            option_home_btn.setTextSize(14);
        }
        option_home_btn.setTextColor(textColor);
        option_home_btn.setVisibility(View.VISIBLE);
    }

    public void setRightIcon(int resId, OnClickListener onClick) {
        if (resId == 0) {
            right_icon.setVisibility(View.GONE);
        } else {
            right_icon.setVisibility(View.VISIBLE);
        }
        right_icon.setImageResource(resId);
        right_icon.setOnClickListener(onClick);
    }

    /**
     * @return void
     * @throws
     * @Title: setTopTitle
     * @Description: 设置显示的标题
     */
    public void setTopTitle(String title) {
        if (title != null && title.length() >= 18) {
            title = title.subSequence(0, 18) + "..";
        }
        bar_title_id.setText(title);
    }

    /**
     * @return void
     * @throws
     * @Title: setTopTitle
     * @Description: 设置显示的标题
     */
    public void setTopTitleSize(float size) {
        bar_title_id.setTextSize(size);
    }

    /**
     * @return void
     * @throws
     * @Title: setTopTitle
     * @Description: 设置显示的标题
     */
    public void setTopTitle(int titleId) {
        setTopTitle(getResources().getString(titleId));
    }


    /**
     * @param
     * @return void
     * @Title: getRightButton
     * @Description: 设置右边按钮的字体大小和文字格式
     */
    public Button getRightButton() {
        return option_home_btn;
    }

}
